package com.demo.function.spring.loadbalancer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 保存当前线程请求灰度标识
 * 使用阿里的threadLocal，使用主线程和子线程实现对象共享
 * @author K
 */
@Slf4j
public class GrayRequestContextHolder {

    public final static String GRAY = "gray";

    public static Boolean getGray() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            log.info("requestAttributes对象为空");
            return false;
        }
        String gray = requestAttributes.getRequest().getHeader(GRAY);
        return Boolean.parseBoolean(gray);
    }
}
